package com.itheima.config;

import com.itheima.mapper.AccountAuthenticationMapper;
import com.itheima.mapper.AccountMessageMapper;
import com.itheima.mapper.SysUserMapper;
import com.itheima.service.impl.AccountAuthenticationServiceImpl;
import com.itheima.service.impl.CommentsServiceImpl;
import com.itheima.service.impl.ProjectReviewProgressServiceImpl;
import com.itheima.service.impl.UserprofileServiceImpl;
import com.itheima.webSocket.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
 
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    private final CommentsServiceImpl commentsService;
    private final ProjectReviewProgressServiceImpl progressService;
    private final AccountAuthenticationServiceImpl authenticationService;
    private final UserprofileServiceImpl userprofileService;
    private final AccountAuthenticationMapper messageMapper;
    @Autowired
    public WebSocketConfig(CommentsServiceImpl commentsService, ProjectReviewProgressServiceImpl progressService,
                           AccountAuthenticationMapper messageMapper,
                           UserprofileServiceImpl userprofileService,
                           AccountAuthenticationServiceImpl authenticationService) {
        this.commentsService = commentsService;
        this.progressService = progressService;
        this.authenticationService = authenticationService;
        this.messageMapper = messageMapper;
        this.userprofileService = userprofileService;
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册 WebSocket 处理器和端点
        registry.addHandler(new WebSocketServer(commentsService), "/hotelReviews/{sid}") // "/ws" 为 WebSocket 的端点
                .setAllowedOrigins("*"); // 允许跨域请求
        registry.addHandler(new AccountMessageWebSocketHandler(progressService),"/progress/review/{sid}").setAllowedOrigins("*");
        registry.addHandler(new AccountAuthenticationWebSocketHandler(authenticationService,messageMapper),"/accountAuthentication/{sid}").setAllowedOrigins("*");
        registry.addHandler(new CustomerWebSocketHandler(messageMapper),"/customerMessage/{sid}").setAllowedOrigins("*");
        registry.addHandler(new UserResetWebSocketHandler(userprofileService),"/userReset/{sid}").setAllowedOrigins("*");
    }
}